<template>
  <div class="dorm-detail-main">
    <el-dialog title="宿舍编辑" :visible.sync="dialogVisible" width="70%" :before-close="handleClose">
      <el-card>
        <div slot="header">
          <span>宿舍信息</span>
        </div>
        <el-form ref="form" :model="dorm" label-width="80px">
          <el-form-item label="宿舍名称">
            <el-input v-model="dorm.name"></el-input>
          </el-form-item>
          <el-form-item label="宿舍类型">
            <el-select v-model="dorm.type" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="宿舍地址">
            <el-input v-model="dorm.position" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="宿舍描述">
            <el-input v-model="dorm.bak" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="时间">
            <el-date-picker
              v-model="dorm.startTime"
              type="date"
              placeholder="选择开始日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
            <el-date-picker
              v-model="dorm.endTime"
              type="date"
              placeholder="选择结束日期"
              style="margin-left:30px"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="doSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { dormDetail, dormSave, dormUpdate } from "@/api/system/dorm";
export default {
  data() {
    return {
      dialogVisible: false,
      dorm: {},
      options: [
        {
          value: "0",
          label: "男",
        },
        {
          value: "1",
          label: "女",
        },
        {
          value: "2",
          label: "未定",
        },
      ],
    };
  },
  created() {},
  methods: {
    handleClose() {
      this.dialogVisible = false;
      this.dorm = {};
    },
    openDialog() {
      this.dialogVisible = true;
    },
    openDialogEdit(id) {
      this.openDialog();
      dormDetail({ id }).then((res) => {
        this.dorm = res.data;
      });
    },
    doSave() {
      var api = null;
      if (this.dorm.id) {
        api = dormUpdate;
      } else {
        api = dormSave;
      }
      api({
        xyDormInfo: {
          ...this.dorm,
        },
      }).then((res) => {
        if (res.code == "error") {
          this.$message.error("数据提交失败");
        } else {
          this.$message.success("保存成功");
          this.handleClose();
          this.$emit("refresh-list"); //使用$emit触发父组件的自定义事件
          console.log(res);
        }

        // this.$message.success("保存成功");
        // this.handleClose();
        // this.$emit("refresh-list");
        // console.log(res);
      });
    },
  },
};
</script>
<style>
</style>